package com.pagoda.viewfinder_compiler.model;

import com.pagoda.viewfinder_annotation.BindClick;
import com.pagoda.viewfinder_annotation.BindView;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;

/**
 * E-mail: liaohailong190@foxmail.com
 * Date: 2020/7/16 17:14
 * Description:
 *
 * @author liaohailong
 */
public class BindViewField {
    private VariableElement fieldElement;
    private int resId;

    public BindViewField(Element element) {
        if (element.getKind() != ElementKind.FIELD) {
            String message = String.format("Only fields can be annotated with @%s", BindClick.class.getSimpleName());
            throw new IllegalArgumentException(message);
        }

        fieldElement = (VariableElement) element;
        resId = fieldElement.getAnnotation(BindView.class).value();

        if (resId < 0) {
            String message = String.format("value() int %s for field %s is not valid !", BindView.class.getSimpleName(), fieldElement.getSimpleName());
            throw new IllegalArgumentException(message);
        }
    }

    public int getResId() {
        return resId;
    }

    public TypeMirror getFieldType() {
        return fieldElement.asType();
    }

    public Name getFieldName() {
        return fieldElement.getSimpleName();
    }

}
